﻿<phone:PhoneApplicationPage
    x:Class="OneShoppingList.MainPage"
    xmlns:local="clr-namespace:OneShoppingList" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
    xmlns:mm="clr-namespace:MetroInMotionUtils"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="696" 
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait"  Orientation="Portrait"
    DataContext="{Binding Main,Source={StaticResource Locator}}"
    shell:SystemTray.IsVisible="True">

    <Grid x:Name="LayoutRoot" Background="Transparent" Margin="0,-12,0,0">
        <Grid
            Visibility="{Binding Shops.Count, Converter={StaticResource toVisibilityConverter}}">
            <controls:Pivot 
                Name="pivot"
                Margin="0,0,-12,0"
                Title="{Binding Localized.applicationName}"
                ItemsSource="{Binding Shops}"
                SelectedItem="{Binding CurrentShop,Mode=TwoWay}"
                SelectionChanged="pivot_SelectionChanged"
                >
                <controls:Pivot.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Name}"/>
                    </DataTemplate>
                </controls:Pivot.HeaderTemplate>
                <controls:Pivot.ItemTemplate>
                    <DataTemplate>
                        <toolkit:LongListSelector 
                            Visibility="{Binding Main.UnsortedShoppingList.Count, Source={StaticResource Locator}, Converter={StaticResource toVisibilityConverter}}"
                            mm:MetroInMotion.IsPivotAnimated="True"
                            ItemsSource="{Binding ShoppingListViewModel.GroupsView}"
                            ScrollingStarted="longListSelector_ScrollingStarted"
                            ScrollingCompleted="longListSelector_ScrollingCompleted"
                            Link="longListSelector_Link"
                            Unlink="longListSelector_Unlink"
                            ListFooter="{Binding}"
                            >

                            <toolkit:LongListSelector.GroupFooterTemplate>
                                <DataTemplate>
                                    <Grid Height="6" Width="468"/>
                                </DataTemplate>
                            </toolkit:LongListSelector.GroupFooterTemplate>
                            

                            <toolkit:LongListSelector.GroupHeaderTemplate>
                                <DataTemplate>
                                    <Button
                                        Style="{StaticResource TiltButtonStyle}"
                                        Height="54"
                                        Margin="0,6,0,0"
                                        Click="CategoryButton_Click"
                                        >
                                        <StackPanel Orientation="Vertical">
                                        <TextBlock Text="{Binding Key,Converter={StaticResource toUpperConverter}}" 
                                                    VerticalAlignment="Center"
                                                    mm:MetroInMotion.AnimationLevel="0"
                                                    Margin="12,0,0,0"
                                                    Style="{StaticResource PhoneTextLargeStyle}"/>
                                        <Border 
                                            Margin="0,0,0,0"
                                            BorderBrush="{StaticResource PhoneForegroundBrush}"
                                            BorderThickness="1"
                                            />
                                        </StackPanel>
                                    </Button>
                                </DataTemplate>
                            </toolkit:LongListSelector.GroupHeaderTemplate>

                            <toolkit:LongListSelector.ItemTemplate>
                                <DataTemplate>
                                    <Grid 
                                        Width="468" 
                                        Background="Transparent"
                                        Tap="Grid_Tap"
                                        mm:MetroInMotion.AnimationLevel="1"
                                        >
                                        <toolkit:ContextMenuService.ContextMenu>
                                            <local:ShoppingItemContextMenu 
                                                OnEdit="EditButton_Click" 
                                                OnRemove="RemoveButton_Click"
                                                OnAddFavorite="ShoppingItemContextMenu_OnAddFavorite"
                                                OnRemoveFavorite="ShoppingItemContextMenu_OnRemoveFavorite"
                                                Opened="menu_Opened"/>
                                        </toolkit:ContextMenuService.ContextMenu>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="72"/>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="Auto"/>
                                        </Grid.RowDefinitions>


                                        <Border 
                                            Margin="0,-3,0,3"
                                            BorderBrush="{StaticResource PhoneChromeBrush}"
                                            BorderThickness="1"
                                            Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3"
                                            />

                                        <TextBlock 
                                            HorizontalAlignment="Left"
                                            Grid.Column="0" Grid.Row="1"
                                            Text="{Binding Caption}"
                                            TextWrapping="NoWrap"
                                            Margin="12,0,60,0"
                                            Style="{StaticResource PhoneTextLargeStyle}"/>

                                        <TextBlock 
                                            HorizontalAlignment="Left"
                                            Grid.Row="2" Grid.Column="0"
                                            Text="{Binding PreferredShop}"
                                            TextWrapping="NoWrap"
                                            Name="PreferredShop"
                                            Margin="12,0,0,0"
                                            Style="{StaticResource PhoneTextSubtleStyle}"/>

                                        <TextBlock
                                            Grid.Row="1" Grid.Column="0"
                                            Text="{Binding DefaultQuantity}"
                                            TextWrapping="NoWrap"
                                            Margin="0,0,3,0"
                                            HorizontalAlignment="Right"
                                            Style="{StaticResource PhoneTextLargeStyle}"
                                            />
                                        <TextBlock
                                            Grid.Row="2" Grid.Column="0"
                                            Text="{Binding UnitSize}"
                                            TextWrapping="NoWrap"
                                            Margin="0,0,3,0"
                                            HorizontalAlignment="Right"
                                            Style="{StaticResource PhoneTextSubtleStyle}"
                                            />

                                        <Button 
                                            Margin="0"
                                            Grid.Row="1"  Grid.RowSpan="2" Grid.Column="1"
                                            Height="72"
                                            Width="72"
                                            Tap="Button_Tap"
                                            Click="ToShoppingBasket_Click"
                                            Style="{StaticResource SampleIconButton}"
                                            Content="{StaticResource checkBrush}"
                                            />
                                    </Grid>
                                </DataTemplate>
                            </toolkit:LongListSelector.ItemTemplate>
                            
                            <toolkit:LongListSelector.ListFooterTemplate>
                                <DataTemplate>
                                    <StackPanel>
                                        <Button
                                            Visibility="{Binding IsTrial, Converter={StaticResource toVisibilityConverter}}"
                                            DataContext="{Binding Settings, Source={StaticResource Locator}}"
                                            Background="{StaticResource PhoneAccentBrush}"
                                            Style="{StaticResource TiltButtonStyle}"
                                            Click="buyButtonClicked"
                                            Height="54"
                                            Margin="0,6,0,0"
                                            >
                                            <TextBlock Text="WINDOWS PHONE APPS" 
                                                    VerticalAlignment="Center"
                                                    mm:MetroInMotion.AnimationLevel="0"
                                                    Margin="12,0,0,0"
                                                    Style="{StaticResource PhoneTextLargeStyle}"/>
                                            <!--<Border 
                                            Margin="0,0,0,0"
                                            BorderBrush="{StaticResource PhoneForegroundBrush}"
                                            BorderThickness="1"
                                            />-->
                                            <!--</StackPanel>-->
                                        </Button>
                                        <Grid 
                                            Width="468" 
                                            Visibility="{Binding IsTrial, Converter={StaticResource toVisibilityConverter}}"
                                            DataContext="{Binding Settings, Source={StaticResource Locator}}"
                                            Background="Transparent"
                                            >
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="*"/>
                                                <ColumnDefinition Width="72"/>
                                            </Grid.ColumnDefinitions>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="Auto"/>
                                                <RowDefinition Height="Auto"/>
                                                <RowDefinition Height="Auto"/>
                                            </Grid.RowDefinitions>

                                            <TextBlock 
                                                HorizontalAlignment="Left"
                                                Grid.Column="0" Grid.Row="1"
                                                Text="One Shopping List"
                                                TextWrapping="NoWrap"
                                                Margin="12,0,60,0"
                                                Style="{StaticResource PhoneTextLargeStyle}"/>

                                            <TextBlock 
                                                HorizontalAlignment="Left"
                                                Grid.Row="2" Grid.Column="0"
                                                Text="Windows Store"
                                                TextWrapping="NoWrap"
                                                Margin="12,0,0,0"
                                                Style="{StaticResource PhoneTextSubtleStyle}"/>

                                            <TextBlock
                                                Grid.Row="1" Grid.Column="0"
                                                Text="1"
                                                TextWrapping="NoWrap"
                                                Margin="0,0,3,0"
                                                HorizontalAlignment="Right"
                                                Style="{StaticResource PhoneTextLargeStyle}"
                                                />
                                            <TextBlock
                                                Grid.Row="2" Grid.Column="0"
                                                Text="License"
                                                TextWrapping="NoWrap"
                                                Margin="0,0,3,0"
                                                HorizontalAlignment="Right"
                                                Style="{StaticResource PhoneTextSubtleStyle}"
                                            />

                                            <Button 
                                                Margin="0"
                                                Grid.Row="1"  Grid.RowSpan="2" Grid.Column="1"
                                                Height="72"
                                                Width="72"
                                                Click="buyButtonClicked"
                                                Style="{StaticResource SampleIconButton}"
                                                Content="{StaticResource checkBrush}"
                                                />
                                            </Grid>
                                        <TextBlock
                                            Margin="12,0,0,0"
                                            Text="{Binding ToggleText}" 
                                            Visibility="{Binding ShowToggleTextInList, Converter={StaticResource toVisibilityConverter}}"
                                            TextWrapping="Wrap"/>
                                        <StackPanel 
                                            Orientation="Vertical"
                                            Visibility="{Binding ShowToggleButtonInList, Converter={StaticResource toVisibilityConverter}}"
                                            >
                                            <HyperlinkButton 
                                                HorizontalAlignment="Left"
                                                Padding="0,6,24,24"
                                                DataContext="{Binding Settings, Source={StaticResource Locator}}"
                                                Command="{Binding ToggleShowAllCommand}"
                                                Content="{Binding ToggleButtonCaption}">
                                            </HyperlinkButton>
                                        </StackPanel>
                                    </StackPanel>
                                </DataTemplate>
                            </toolkit:LongListSelector.ListFooterTemplate>
                        </toolkit:LongListSelector>
                    </DataTemplate>
                </controls:Pivot.ItemTemplate>
            </controls:Pivot>
            <StackPanel Margin="0,17,12,28" 
                        HorizontalAlignment="Right" 
                        VerticalAlignment="Top" 
                        Orientation="Horizontal">
                <TextBlock Text="{Binding CurrentShop.ShoppingListViewModel.ItemsView.Count}" />
                <TextBlock Text=" / " />
                <TextBlock Text="{Binding UnsortedShoppingList.Count}" />
                <TextBlock Text="{Binding Localized.itemsName}" />
            </StackPanel>
            <StackPanel Orientation="Vertical" 
                Margin="12,152,12,0"
                Visibility="{Binding UnsortedShoppingList.Count, Converter={StaticResource toVisibilityInvertor}}"
                >
                <TextBlock 
                    Text="{Binding Localized.yourListEmpty}"
                    Style="{StaticResource PhoneTextLargeStyle}"
                    VerticalAlignment="Top"/>
                <Button Style="{StaticResource TiltButtonStyle}" 
                    Click="appAddButton_Click"
                    BorderThickness="0" Padding="0" Margin="24,12,0,0">

                    <StackPanel Orientation="Horizontal">
                        <Border VerticalAlignment="Center" 
                                Margin="0"
                                HorizontalAlignment="Center" 
                                Height="48"
                                Width="48"
                                BorderBrush="{StaticResource PhoneForegroundBrush}" 
                                CornerRadius="21" BorderThickness="3">
                            <Grid Height="48" Width="48"
                                  HorizontalAlignment="Center"
                                  VerticalAlignment="Center"
                                  OpacityMask="{StaticResource addBrush}" 
                                  Background="{StaticResource PhoneForegroundBrush}" >
                            </Grid>
                        </Border>
                        <TextBlock
                            Text="{Binding Localized.addItemManually}"
                            VerticalAlignment="Center"
                            Style="{StaticResource PhoneTextLargeStyle}"
                        />
                    </StackPanel>
                </Button>
                <Button Style="{StaticResource TiltButtonStyle}" 
                    Click="appbarFavorits_Click"
                    BorderThickness="0" Padding="0" Margin="24,18,0,0">

                    <StackPanel Orientation="Horizontal">
                        <Border VerticalAlignment="Center" 
                                Margin="0"
                                HorizontalAlignment="Center" 
                                Height="48"
                                Width="48"
                                BorderBrush="{StaticResource PhoneForegroundBrush}" 
                                CornerRadius="21" BorderThickness="3">
                            <Grid Height="48" Width="48"
                                  HorizontalAlignment="Center"
                                  VerticalAlignment="Center"
                                  OpacityMask="{StaticResource favoritesBrush}" 
                                  Background="{StaticResource PhoneForegroundBrush}" >
                            </Grid>
                        </Border>
                        <TextBlock
                            Text="{Binding Localized.checkInList}"
                            VerticalAlignment="Center"
                            Style="{StaticResource PhoneTextLargeStyle}"
                        />
                    </StackPanel>
                </Button>
            </StackPanel>
            <Button Content="About"
                    x:Name="aboutButton"
                    Click="aboutButton_Click"
                    Margin="24,0,12,64"
                    VerticalAlignment="Bottom"
                    Visibility="{Binding UnsortedShoppingList.Count, Converter={StaticResource toVisibilityInvertor}}"/>
        </Grid>

        <Grid x:Name="RootOverlay" 
              Background="Transparent"
              Visibility="{Binding Shops.Count, Converter={StaticResource toVisibilityInvertor}}"
            >
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <!--TitlePanel contains the name of the application and page title-->
            <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
                <TextBlock x:Name="ApplicationTitle" Text="{Binding Localized.applicationName}" 
                           Style="{StaticResource PhoneTextNormalStyle}"/>
                <TextBlock x:Name="PageTitle" Text="{Binding Localized.initialSetupPageTitle}" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
            </StackPanel>

            <!--ContentPanel - place additional content here-->
            <ScrollViewer x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
                <StackPanel Orientation="Vertical"
                            DataContext="{Binding Settings, Source={StaticResource Locator}}">
                    <StackPanel Orientation="Vertical"
                            Visibility="{Binding IsUserKnown, Converter={StaticResource toVisibilityInvertor}}">
                            <TextBlock Margin="12,0,0,0"
                               Text="{Binding Localized.syncDescription}"
                               TextWrapping="Wrap"
                               Style="{StaticResource PhoneTextNormalStyle}"/>
                        <Button x:Name="syncButton"
                            Margin="12,12,0,12" Content="{Binding Localized.syncButton}"
                            Click="syncButton_Click"/>
                        <Border 
                        Margin="12,0,12,0"
                        BorderBrush="{StaticResource PhoneInactiveBrush}"
                        BorderThickness="1"
                        Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3"
                        />
                    </StackPanel>
                    <StackPanel Orientation="Vertical" 
                                DataContext="{Binding Main, Source={StaticResource Locator}}"
                                Visibility="{Binding AllItemsViewModel.ItemsView.Count, Converter={StaticResource toVisibilityInvertor}}">
                    <TextBlock TextWrapping="Wrap" 
                           Margin="12,12,0,0"
                           Style="{StaticResource PhoneTextNormalStyle}"
                           Text="{Binding Localized.importDescription}" />
                    <toolkit:ListPicker Name="picker" 
                                        Margin="24,6,12,0"
                                        Header="{Binding Localized.importPicker}">
                        <toolkit:ListPickerItem Content="Deutsch (metrisch)"/>
                        <toolkit:ListPickerItem Content="English (US customary units)"/>
                        <toolkit:ListPickerItem Content="English (metric)"/>
                    </toolkit:ListPicker>
                    <TextBlock Margin="24,0,12,0" Text="ALDI, KAUFLAND" 
                    Style="{StaticResource PhoneTextSubtleStyle}"
                    Visibility="{Binding SelectedIndex, ElementName=picker,
                    Converter={StaticResource nToVisibilityConverter},ConverterParameter=0}"/>
                    <!--<TextBlock Margin="24,0,12,0" Text="Getränke, Früchte und Gemüse, Kühlregal, ..."
                    Style="{StaticResource PhoneTextSubtleStyle}"
                    Visibility="{Binding SelectedIndex, ElementName=picker,
                    Converter={StaticResource nToVisibilityConverter},ConverterParameter=0}"/>-->
                    <TextBlock Margin="24,0,12,0" Text="Kilo, Liter, Becher, Beutel, Packung, Stück, ..."
                    Style="{StaticResource PhoneTextSubtleStyle}"
                    Visibility="{Binding SelectedIndex, ElementName=picker,
                    Converter={StaticResource nToVisibilityConverter},ConverterParameter=0}"/>
                    <TextBlock Margin="24,0,12,0" Text="ALDI, WALMART" 
                    Style="{StaticResource PhoneTextSubtleStyle}"
                    Visibility="{Binding SelectedIndex, ElementName=picker,
                    Converter={StaticResource nToVisibilityConverter},ConverterParameter=1}"/>
                    <!--<TextBlock Margin="24,0,12,0" Text="Beverages, Canned &amp; Packed Foods, Frozen Fo..."
                    Style="{StaticResource PhoneTextSubtleStyle}"
                    Visibility="{Binding SelectedIndex, ElementName=picker,
                    Converter={StaticResource nToVisibilityConverter},ConverterParameter=1}"/>-->
                    <TextBlock Margin="24,0,12,0" Text="lbs, oz, can, pack, bottle, pcs, ..."
                    Style="{StaticResource PhoneTextSubtleStyle}"
                    Visibility="{Binding SelectedIndex, ElementName=picker,
                    Converter={StaticResource nToVisibilityConverter},ConverterParameter=1}"/>
                    <TextBlock Margin="24,0,12,0" Text="ALDI, WALMART" 
                    Style="{StaticResource PhoneTextSubtleStyle}"
                    Visibility="{Binding SelectedIndex, ElementName=picker,
                    Converter={StaticResource nToVisibilityConverter},ConverterParameter=2}"/>
                    <!--<TextBlock Margin="24,0,12,0" Text="Beverages, Canned &amp; Packed Foods, Frozen Fo..."
                    Style="{StaticResource PhoneTextSubtleStyle}"
                    Visibility="{Binding SelectedIndex, ElementName=picker,
                    Converter={StaticResource nToVisibilityConverter},ConverterParameter=2}"/>-->
                    <TextBlock Margin="24,0,12,0" Text="kilo, liter, can, pack, bottle, pcs, ..."
                    Style="{StaticResource PhoneTextSubtleStyle}"
                    Visibility="{Binding SelectedIndex, ElementName=picker,
                    Converter={StaticResource nToVisibilityConverter},ConverterParameter=2}"/>
                    <Button x:Name="importButton"
                        Margin="12,12,0,12" Content="{Binding Localized.importButton}"
                        IsEnabled="{Binding SyncHandler.IsRunning, Converter={StaticResource boolNegationConverter}}"
                        Click="importButton_Click"/>
                    <Border 
                        Margin="12,0,0,12"
                        BorderBrush="{StaticResource PhoneInactiveBrush}"
                        BorderThickness="1"
                        Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3"
                        />
                    </StackPanel>
                    <StackPanel Orientation="Vertical"
                                DataContext="{Binding Main, Source={StaticResource Locator}}">
                        <TextBlock Margin="12,12,0,0"
                                   Text="{Binding Localized.scratchDescription}"
                                   Visibility="{Binding AllItemsViewModel.ItemsView.Count, Converter={StaticResource toVisibilityInvertor}}"                               
                                   TextWrapping="Wrap"
                               Style="{StaticResource PhoneTextNormalStyle}"/>
                        <TextBlock Margin="12,12,0,0"
                                   Text="{Binding Localized.scratchDescription1}"
                                   Visibility="{Binding AllItemsViewModel.ItemsView.Count, Converter={StaticResource toVisibilityConverter}}"                               
                                   TextWrapping="Wrap"
                               Style="{StaticResource PhoneTextNormalStyle}"/>
                        <Button x:Name="addPathButton"
                                IsEnabled="{Binding SyncHandler.IsRunning, Converter={StaticResource boolNegationConverter}}"                                
                                Margin="12,12,0,24" Content="{Binding Localized.scratchButton}"
                                Click="addPathButton_Click"/>
                    </StackPanel>
                </StackPanel>
            </ScrollViewer>
        </Grid>

    </Grid>
    
    <shell:SystemTray.ProgressIndicator>

        <shell:ProgressIndicator
            x:Name="progressIndicator" 
            Text="{Binding SyncHandler.CurrentOperation}"
            IsVisible="{Binding SyncHandler.IsRunning}"
            IsIndeterminate="{Binding SyncHandler.IsRunning}"/>

    </shell:SystemTray.ProgressIndicator>

    <!--Sample code showing usage of ApplicationBar-->
    <phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True" x:Name="appBar">
            <shell:ApplicationBarIconButton x:Name="appbar_sendEmail" IconUri="/Images/appbar.feature.email.rest.png" Text="email" Click="appbar_sendEmail_Click" />
            <shell:ApplicationBarIconButton x:Name="appbar_add" IconUri="/Images/appbar.add.rest.png" Text="add" Click="appAddButton_Click" />
            <shell:ApplicationBarIconButton x:Name="appbar_favorites" IconUri="/Images/appbar.favs.rest.png" Text="favorites" Click="appbarFavorits_Click" />
            <shell:ApplicationBarIconButton x:Name="appbar_sync" IconUri="/Images/appbar.sync.rest.png" Text="sync" Click="appbar_sync_Click"/>
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem x:Name="appbar_clearList" Text="clear my shopping list" Click="appbar_clearList_Click"/>
                <shell:ApplicationBarMenuItem x:Name="appbar_shopsConfig" Text="edit shopping paths" Click="ShopsConfiguration_Click" />
                <shell:ApplicationBarMenuItem x:Name="appbar_more" Text="settings and more" Click="more_Click" />
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>

</phone:PhoneApplicationPage>